﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.Entities;
using vensoft.BussinesLogic;

namespace vensoft.Win32
{
    public partial class frmAEProveedor : Form
    {
        IProveedorBL proveedorBL;

        public frmAEProveedor()
        {
            InitializeComponent();
            proveedorBL = new ProveedorBL();
        }

        public Proveedor currentProvider { get; set; }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void Editar()
        {
            proveedorBindingSource.DataSource = currentProvider;
        }
             
        public void Nuevo()
        {
            Proveedor currentProvider = proveedorBindingSource.Current as Proveedor;
            proveedorBindingSource.AddNew();
            currentProvider = null;
        }
        private void btnEditarProveedor_Click(object sender, EventArgs e)
        {
            if (rUCTextBox.Text.Trim().Length == 11)
            {
                if (MessageBox.Show("¿Estas seguro que deseas guardar?", "ALERTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Proveedor currentProvider = proveedorBindingSource.Current as Proveedor;
                    proveedorBL.RegistrarProveedor(currentProvider);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("El RUC debe tener 11 digitos!");
            }
        }

        #region Validaciones
        private void txtSoloTexto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtSoloNumeros_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;

            }
        }

        private void RUCTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(rUCTextBox);
        }

        private void razon_socialTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(razon_socialTextBox);
        }

        private void descripcionTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(descripcionTextBox);
        }

        private void telefonoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(telefonoTextBox);
        }

        private void ErrorProvider(TextBox T)
        {
            if (T.Text.Trim().Length == 0)
            {
                erpProveedor.SetError(T, "Debe Colocar un Dato");
                btnEditarProveedor.Enabled = false;
            }

            else
            {
                erpProveedor.SetError(T, "");
                btnEditarProveedor.Enabled = true;
            }
        }

        #endregion

        private void frmAEProveedor_Load(object sender, EventArgs e)
        {
            if (rUCTextBox.Text.Trim().Length == 0)
                btnEditarProveedor.Enabled = false;
            else
                btnEditarProveedor.Enabled = false;
        }

    }
}
